﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.SqlClient;
using System.Data;

namespace ASPNETVN.PORTAL.Components.Security
{
    public partial class Profile
    {
        public string GetPropertyValue(int userID, string propertyName)
        {
            const string spName = "[dbo].[p_SYSTEM_Profile_GetPropertyValueBy_UserID_PropertyName]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@UserID", SqlDbType.Int, userID);
            db.AddInParameter(dbCommand, "@PropertyName", SqlDbType.NVarChar, propertyName);
            Profile entity = null;
            IDataReader reader = db.ExecuteReader(dbCommand);
            if (reader.Read())
            {
                entity = new Profile();
                if (!reader.IsDBNull(reader.GetOrdinal("ID"))) entity.ID = reader.GetInt32(reader.GetOrdinal("ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("UserID"))) entity.UserID = reader.GetInt32(reader.GetOrdinal("UserID"));
                if (!reader.IsDBNull(reader.GetOrdinal("PropertyName"))) entity.PropertyName = reader.GetString(reader.GetOrdinal("PropertyName"));
                if (!reader.IsDBNull(reader.GetOrdinal("PropertyValue"))) entity.PropertyValue = reader.GetString(reader.GetOrdinal("PropertyValue"));
            }
            reader.Close();
            return entity == null ? string.Empty : entity.PropertyValue;
        }

        public IList<Profile> SelectCollectionBy_UserID(int userID)
        {
            const string spName = "[dbo].[p_SYSTEM_Profile_SelectBy_UserID]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@UserID", SqlDbType.Int, userID);
            IList<Profile> collection = new List<Profile>();
            IDataReader reader = db.ExecuteReader(dbCommand);
            while (reader.Read())
            {
                Profile entity = new Profile();
                if (!reader.IsDBNull(reader.GetOrdinal("ID"))) entity.ID = reader.GetInt32(reader.GetOrdinal("ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("UserID"))) entity.UserID = reader.GetInt32(reader.GetOrdinal("UserID"));
                if (!reader.IsDBNull(reader.GetOrdinal("PropertyName"))) entity.PropertyName = reader.GetString(reader.GetOrdinal("PropertyName"));
                if (!reader.IsDBNull(reader.GetOrdinal("PropertyValue"))) entity.PropertyValue = reader.GetString(reader.GetOrdinal("PropertyValue"));

                collection.Add(entity);
            }
            reader.Close();
            return collection;
        }
    }
}